/**
 */
;
(function(){
'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningAuditWarnCtrl', ['$scope','$modal','$state', 
	                                  '$stateParams','WarningAddService','BaseURL','Modal','WarningScreenService','FwaTabsetService','CommunityService',
	    function ($scope,$modal,$state,$stateParams,WarningAddService,baseUrl,Modal,WarningScreenService,FwaTabsetService,CommunityService) {
		 $scope.communityService = CommunityService;
	        CommunityService.initCodeListDatas();
	        $scope.communityService = CommunityService;
	        var currentDeadLine = '';
	        $scope.$watch('communityService.caseDate',function(v){
	                angular.forEach(v,function(data,index){
	                if(data.code=='02')
	                {
	                    var caseLimitDate = parseInt(data.name);
	                    CommunityService.initDate("yyyy-mm-dd").success(function(res){
	                        var currentDate = new Date(res.formatDate);
	                        currentDate.setDate(currentDate.getDate() + caseLimitDate);
	                        currentDeadLine = currentDate.format('yyyy-MM-dd');
	                    });
	                }
	            });
	        });
		var warnNo=$stateParams.warnNo,
			staffCode=[],
			insurCode=[],
		    screen=[];
		$scope.saveDialogload=true;
		$scope.warningFrom={};
		$scope.warningFrom.date={};
		$scope.notice = {};
		$scope.warnInsurCount=0;
		$scope.warnStaffCount=0;
		$scope.warnMedicalCount=0;
		$scope.warnInsurData=[];
		$scope.warnStaffData=[];
		$scope.warnMedicalData={};
		$scope.warnScreenList=[];
		$scope.prcoessTypeFlag=false;
		function queryWarning(){
			WarningAddService.getHcpmsWarning({warnNo:warnNo})
			.success(function(res){
					$scope.warningFrom=res.warnInfo;
					if($scope.warningFrom.auditOpinion!=null){
						$scope.warningFrom.audit=$scope.warningFrom.auditOpinion+'|'+$scope.warningFrom.auditName;
					}
					if(res.warnStaffList){
						$scope.warnStaffData=res.warnStaffList;
						$scope.warnStaffCount=res.warnStaffList.length;
						angular.forEach($scope.warnStaffData,function(data,index){
							 if($.inArray(data.code, staffCode) === -1){
								 staffCode.push(data.code);
							 }
						 });
			         }
					if(res.warnInsuranceList){
						$scope.warnInsurData=res.warnInsuranceList;
						$scope.warnInsurCount=res.warnInsuranceList.length;
						angular.forEach($scope.warnInsurData,function(data,index){
							if($.inArray(data.code, insurCode) === -1){
								insurCode.push(data.code);
							 }
						 });
			          }
					if(res.warnScreenList){
						$scope.warnScreenList=res.warnScreenList;
						angular.forEach($scope.warnScreenList,function(data,index){
							screen.push(data.monitorNo+data.ruleNo);
						 });
					 }
					if(res.notice){
						$scope.notice=res.notice;
					}
	        		if(res.warnMedicalInfo){
	        			$scope.warnMedicalData=res.warnMedicalInfo;
	        			$scope.warnMedicalCount=1;
	        		}
	        		if($scope.warningFrom.process==='02'||$scope.warningFrom.process==='04'){
        				$scope.prcoessTypeFlag=true;
        			}
	        		if($scope.warningFrom.auditOpinion === '01'&&($scope.warningFrom.process === '02'||$scope.warningFrom.process === '04')){	
	        			$scope.feedbackDate=true;
	        		}
	        		//更新当前页面数据
        		   WarningScreenService.findPointCutTempScose(
        				   WarningAddService.warningMarks,$stateParams.warnNo,'01',
        				   $scope.warnMedicalData,$scope.warnStaffData,$scope.warnInsurData);
		     });
		}
		//查看案件来源
		$scope.viewDetailWarning = function(){
			var scope = $scope.$new();
			scope.warningFrom = $scope.warningFrom;
			var modalInstance = $modal.open({
	      	      templateUrl: baseUrl +'views/hcpms/warning/warnView/warningViewBase_view.html',
	      	      backdrop:'static',
				  size:'sm',
				  scope:scope,
	      	      controller:function($modalInstance,$scope){
					  $scope.onOk = function(){
				  		   $modalInstance.dismiss();
				  	   }	
				  }
	  	    });	
		}
		//自查通知书
		$scope.warningNoticeDialog = function() {
			var scope = $scope.$new();
			scope.warnNotice=$scope.notice;
			var modalInstance = $modal.open({
	      	      templateUrl: baseUrl +'views/hcpms/warning/warnEdit/warningEditNoticeDialog_view.html',
	      	      backdrop:'static',
				  size:'sm',
				  scope:scope,
	      	      controller:'WarningAddNoticeCtrl'	      	      
		      	});
			modalInstance.result.then(function(item) {
				$scope.notice=item;
			});
		}
		function downFile(param){
			window.location = baseUrl+'HcpmsWarnDownFile/downPdfFile.do?warnNo='+warnNo+'&docType='+param+'&warnNoBiz='+$scope.warningFrom.warnNoBiz;
		}
		//新增参保人员
		$scope.addInsurance = function() {
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningAddInsurDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  controller: 'WarningAddInsurCtrl'
			    });
			 modalInstance.result.then(function(item) {
				 angular.forEach(item,function(data,index){
					 if($.inArray(data.code, insurCode) === -1){
						 $scope.warnInsurData.push(data);
						 insurCode.push(data.code);
					 }
				 });
				 $scope.warnInsurCount=$scope.warnInsurData.length;
	 	    });
		}
		//删除参保人员
		$scope.deleteInsurance = function (data){
			Modal.confirm('确认删除参保人'+data.name+'吗？',function(){
				WarningScreenService.deleteMark(WarningAddService.warningMarks,3,data);//mark:1.机构，2.医务人员，3.参保人
				$scope.warnInsurData.splice($.inArray(data,$scope.warnInsurData),1);
				 insurCode.splice($.inArray(data.code,insurCode),1);
				$scope.warnInsurCount=$scope.warnInsurData.length;
			});
		}
		//新增医护人员
		$scope.addStaff = function() {
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningAddStaffDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  controller: 'WarningAddStaffCtrl'
			    });
			 //接收返回数据
			 modalInstance.result.then(function(item) {
				 angular.forEach(item,function(data,index){
					 if($.inArray(data.code, staffCode) === -1){
						 $scope.warnStaffData.push(data);
						 staffCode.push(data.code);
					 }
				 });
				 $scope.warnStaffCount=$scope.warnStaffData.length;
	 	    });
		}
		//删除医护人员
		$scope.deleteStaff = function (data){
			Modal.confirm('确认删除医护人员'+data.name+'吗？',function(){
				WarningScreenService.deleteMark(WarningAddService.warningMarks,2,data);//mark:1.机构，2.医务人员，3.参保人
				$scope.warnStaffData.splice($.inArray(data,$scope.warnStaffData),1);
				staffCode.splice($.inArray(data.code,staffCode),1);
				$scope.warnStaffCount=$scope.warnStaffData.length;
			});
		}
		//查看参保人员详细信息
		$scope.viewInsurList = function(){
			var scope = $scope.$new();
			scope.warnInsuranceList=$scope.warnInsurData;
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnView/warningViewInsurance_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller:function($modalInstance,$scope){
					  $scope.onOk = function(){
				  		   $modalInstance.dismiss();
				  	   }	
				  }
			    });
			
		}
		//查看医护人员详细信息
		$scope.viewStaffList = function(){
			var scope = $scope.$new();
			scope.warnStaffList=$scope.warnStaffData;
			var modalInstance = $modal.open({
				  templateUrl: baseUrl+'views/hcpms/warning/warnView/warningViewStaff_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller:function($scope,$modalInstance){
		  	          //取消或或者直接关闭
					  $scope.onOk = function(){
						  $modalInstance.dismiss();
			          }	
				  }
			    });
			
		}
		//查看医疗机构详细信息
		$scope.viewMedicalInfo = function(){
            if(!$scope.warnMedicalData){
            	return;
            }
			var scope = $scope.$new();
			$scope.medical=$scope.warnMedicalData;
			var modalInstance = $modal.open({
				  templateUrl: baseUrl+'views/hcpms/warning/warnView/warningViewMedical_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller:function($scope,$modalInstance){
		  	          //取消或或者直接关闭
					  $scope.onOk = function(){
						  $modalInstance.dismiss();
			          }	
				  }
			    });
			
		}
		//隐藏扣分以及删除按钮
		$scope.mouseoutShow = function(data){
			data.show = false;
		}
		
        //显示扣分以及删除按钮
		$scope.mouseoverShow = function(data){
			data.show = true;
		}
		/****tab页面*/
		$scope.currentTab = 0;
		$scope.currentTabIndex = 0;
		$scope.tabs=[{name:'网上筛查',status:1,index:0,url:'views/hcpms/warning/warnEdit/warningEditAuditScreen_view.html'}];
		$scope.tabClick = function(index) {
			$scope.currentTab = index;
			$scope.currentTabIndex=index;
		}
		var  approveType=['warn_approve'];
		$scope.approveList=[];
		function init(){
			WarningAddService.getHcpmsCodeList(approveType).success(function(response){
	            $scope.approveList=response.result;
	            queryWarning();
			});
			
		}
		init();
		$scope.feedbackDate=false;
		//新增网上筛查
		$scope.addScreen = function() {
			//打开弹出框
			var modalInstance = $modal.open({
	      	      templateUrl: baseUrl + 'views/hcpms/warning/warnEdit/warningAddScreenDialog_view.html',
	      	      backdrop:'static',
				  size:'sm',
	      	      controller:'WarningAddScreenCtrl'
		      	});
			 //接收返回数据
			 modalInstance.result.then(function(item) { 
				 if($.inArray(item.monitorNo+item.ruleNo, screen) != -1){
					 return;
				 }
				 screen.push(item.monitorNo+item.ruleNo);
				 $scope.warnScreenList.push(item);
	  	    });
		}
		//全选或者不全选
		$scope.checkRows = [];
		$scope.checked = false;
		$scope.getWarnScreenIdList = function (checked){
			angular.forEach($scope.warnScreenList,function(data,index){
				data.checked = checked;
			});
		}
		//删除网上筛查
		$scope.deleteScreen = function() {
			if(!$scope.warnScreenList){
				return;
			}
			var data=[];
			Modal.confirm('确认删除网上筛查吗？',function(){
				$.each($scope.warnScreenList, function(){
					if (this.checked===true) {
						screen.splice($.inArray(this.monitorNo+this.ruleNo,screen),1);						
					}else{
						data.push(this);
					}
				});
				$scope.warnScreenList=data;
			});
		}
		$scope.showFeedbackDate = function(){
			if($scope.warningFrom.audit){
				$scope.warningFrom.auditName=$scope.warningFrom.audit.split("|")[1];
				$scope.warningFrom.auditOpinion=$scope.warningFrom.audit.split("|")[0];
			}else{
				$scope.warningFrom.auditName='';
				$scope.warningFrom.auditOpinion='';
			}
			if($scope.warningFrom.auditOpinion === '01'&&($scope.warningFrom.process === '02'||$scope.warningFrom.process === '04')){
				$scope.feedbackDate=true;
				$scope.warningFrom.feedbackEndDate = currentDeadLine;
			}else{
				$scope.feedbackDate=false;
				$scope.warningFrom.feedbackEndDate='';
			}
			
		}
		 //暂存
		   $scope.saveWarning = function() { 
	           var params=getParams();
	           $scope.saveDialogload=true;
			   WarningAddService.saveHcpmsWarning(params).success(function(data){
				   $scope.saveDialogload=false;
				   Modal.alert(data.msg);
				   if(data.state=='0'){
					   WarningAddService.warningMarks = [];//初始化扣分参数
					   queryWarning();
				   }
			   });
		   }
		   //验证信息
		   function isWarningNull(){
			   var dateReg =/^\d{4}-\d{2}-\d{2}$/; 
			   if(!$scope.warningFrom.auditOpinion){
					Modal.alert("审核意见不能为空!");
					return false;
				}
				if($scope.warningFrom.auditOpinion === '02'&&!$scope.warningFrom.auditDesc){
					Modal.alert("不同意时,意见说明不能为空!");
					return false;
				}
				if($scope.warningFrom.auditOpinion === '01'&&($scope.warningFrom.process === '02'||$scope.warningFrom.process === '04')){
					if(!dateReg.test($scope.warningFrom.feedbackEndDate)){
						Modal.alert("处理方式为自查整改,审核意见同意时,反馈截止时间不能为空,并且必须输入正确的日期格式(yyyy-MM-dd)！");
						return false;
					}
				}
				if($scope.warningFrom.process === '02'||$scope.warningFrom.process === '04'){
					   if(!$scope.notice){
						   //Modal.alert("处理方式为自查整改时，请填写预警通知单!");
						   Modal.alert("请填写预警通知单!");
						   return false;
					   }
					   if(!$scope.notice.content||!$scope.notice.datestart||!$scope.notice.dateend||!$scope.notice.requirement){
						   //Modal.alert("处理方式为自查整改时，没有填写预警通知单或没有填写完全,请完善!");
						   Modal.alert("没有填写预警通知单或没有填写完全,请完善!");
						   return false;
					   }
					   if($scope.notice.name!=$scope.warnMedicalData.name){
						   $scope.notice.name=$scope.warnMedicalData.name
					   }
				   }else{
					   $scope.notice={};
				   }
			   return true;
		   }
		   //保存和暂存设置保存的内容
		   function getParams(){
		           var notice={},
				       staffList=[],
				       insurList=[];
				   angular.forEach($scope.warnStaffData,function(data,index) {
					   var staff={};
					   if(data.code){
						   staff.code = data.code;
					   }
					   if(data.name){
						   staff.name = data.name;
					   }
					   staffList.push(staff);
				   });
				   angular.forEach($scope.warnInsurData,function(data,index) {
					   var insurance={};
					   if(data.code){
						   insurance.code = data.code;
					   }
					   if(data.name){
						   insurance.name = data.name;
					   }
					   insurList.push(insurance);
				   });
		           
		           var param={
		        		     warnNo:warnNo,
		        		     status:$scope.warningFrom.status,
		        		     process:$scope.warningFrom.process,
		        		     agent:$scope.warningFrom.agent,
						     auditOpinion:$scope.warningFrom.auditOpinion,
						     auditName:$scope.warningFrom.auditName,
						     feedbackEndDate:$scope.warningFrom.feedbackEndDate,
						     auditDesc:$scope.warningFrom.auditDesc
						     };
		           var params={
		        		      warnNo:warnNo,
		        		      status:$scope.warningFrom.status,
		        		      warnInfo:param,
		        		      screenList:$scope.warnScreenList,
		        		      staffList:staffList,
		        		      insurList:insurList,
		        		      notice:$scope.notice,
		        		      warnMarkTempDetails:WarningAddService.warningMarks
		                        }; 
		          return params;
		   }
		   //保存
		   $scope.submitWarning = function(){
			   if(!isWarningNull()){
				  return;
			   }		  
	           var params=getParams(); 
	           if($scope.warningFrom.auditOpinion === '02')
	        	   params.warnInfo.isCheckBack="1";
	           else
	        	   params.warnInfo.isCheckBack="0";
	           $scope.saveDialogload=true;
	           WarningAddService.auditWarnProcess(params).success(function(data){
	        	   $scope.saveDialogload=false;
	        	   Modal.alert(data.msg);
				   if(data.state=='0') {
					   WarningAddService.warningMarks = [];//初始化扣分参数
					   if($stateParams.urlFlag){
					   		var urlFlag=$stateParams.urlFlag;
							FwaTabsetService.closeTab('warnQueryAudit','workingArea.warnAuditPost',{urlFlag:urlFlag});
					   	}else{
				   			FwaTabsetService.selectTab("warnQueryAudit.warningAuditList",null,false);
					   	}                
	               }
			   });
		   }
	   $scope.backWarning = function(){
		   	WarningAddService.warningMarks = WarningAddService.warningMarkTemps;//初始化扣分参数
		   	if($stateParams.urlFlag){
		   		var urlFlag=$stateParams.urlFlag;
				FwaTabsetService.closeTab('warnQueryAudit','workingArea.warnAuditPost',{urlFlag:urlFlag});
		   	}else{
	   			FwaTabsetService.selectTab("warnQueryAudit.warningAuditList",null,false);
		   	}
        }
	   
	 //扣分 mark：1.机构扣分，2.医务人员扣分，3参保人员扣分,data:数据集合
	   $scope.marking = function(mark,data,medicalCode,orgType){
		   var medicalCodeTemp=null;
			if(medicalCode&&!angular.isUndefined(medicalCode)){
				medicalCodeTemp = medicalCode;
			}
		   if(mark != 1 && medicalCodeTemp == null){
			   Modal.alert("请先增加医疗机构信息！");
				return null;
		   }else{
			   medicalCode = medicalCodeTemp;
		   }
		   markData(mark,data,medicalCode,orgType);//扣分数据参数加载
		   var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningDeductionDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope: $scope,
//				  scope:scope,
				  controller: 'WarningDeductionDialogCtrl'
			    });
			 //接收返回数据
			 modalInstance.result.then(function(item) {
				 if (!WarningAddService.warningMarks) {
					 WarningAddService.warningMarks = [];
					}
				 WarningAddService.warningMarks = item;
				 //更新当前页面数据
				 WarningScreenService.findPointCutTempScose(item,$stateParams.warnNo,'01',
						   $scope.warnMedicalData,$scope.warnStaffData,$scope.warnInsurData); 
	 	    });
	   }
	   //扣分数据参数加载
	   var markData = function(mark,data,medicalCode,orgType){
		   WarningAddService.mark = mark;
		   WarningAddService.warningMarkData = {
				   orgType:orgType,//0.机构，1.药店
				   medicalCode:medicalCode,//medicalCode:所属机构编号
				   code:data.code,//code:编号
				   name:data.name,//name:名称
				   caseType:'01',//案件类型caseType：01.预警,02.稽核
				   caseNo:$stateParams.warnNo,//案件编号
				   happenDate:$scope.warningFrom.warnDate,//案发日期
				   punishDateStart:$scope.warningFrom.startDate,// 惩罚起期
				   punishDateEnd:$scope.warningFrom.endDate,// 惩罚终期
				   warningMarks:WarningAddService.warningMarks//扣分项数据
				   };
	   }
	   $scope.saveDialogload=false;
	}]);
})();